// fenxiao/index/index.js
var app = getApp();
const utils = require('../../utils/pay.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lists:{},
    platform:"",
    showShare: false, //是否显示规则弹框
    showModel: false, //是否显示充值弹框
    inputMoney:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      platform: app.globalData.platform
    })
  },


  //跳转Ai后台
  toAccountSet: function () {
    var url = 'setAccount.html';
    wx.navigateTo({
      url: '/pages/h5page/index?url=' + url,
    })
  },

  showModel: function () {
    this.setData({ showModel: !this.data.showModel })
  },

  bindInputValue: function (e) {
    this.setData({
      inputMoney: parseFloat(e.detail.value)
    })
  },
  
  checkInput: function(){
    if (!this.checkMoney(this.data.inputMoney)) {
      wx.showModal({
        title: '提示',
        content: '请输入50-9900整数',
        showCancel: false,
      })
      return
    }
  },

  //获取分销商信息，如果分销商不存在，则创建新的
  GetDistributorInfo: function () {
    var that = this;
    var disId = wx.getStorageSync('disid')
    var agentId = wx.getStorageSync('agentid')
    if (disId == "" || disId == null){
        disId = 0
    }
    if (agentId == "" || agentId == null) {
        agentId = 0
    }
    wx.request({
      url: app.globalData.host + '/api/Distributor/GetDistributorAndCreate',
      data: {
        userId: app.globalData.userInfo.Uid,
        disId: disId,
        agentId: agentId,
        storeId: app.globalData.storeId
      },
      success: function (res) {
        if (res.data.Code == 0) {
          var list = res.data.Data;
          list.accumulated = (parseFloat(list.AccountBalance) + parseFloat(list.RequestBalance)).toFixed(2)//累计收益 = 账户余额 + 已提现金额
          that.setData({
            lists: list,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  toTixian:function(){//提现
    wx.navigateTo({
      url: '/fenxiao/tixian/index?money=' + this.data.lists.AccountBalance,
    })
  },
  toCode:function () {//二维码
    wx.navigateTo({
      url: '/fenxiao/shareCodes/index?userid=' + app.globalData.userInfo.Uid,
    })
  },
  toCardCode:function(){//名片二维码
    var url = encodeURI('myCard.html');
    wx.navigateTo({
      url: '/pages/h5page/index?url=' + url,
    })
  },
  //苹果升级申请申请余额代充
  requestAccountBalance: function () {
    wx.showModal({
      title: '提示',
      content: '发送升级申请，保障账户安全！\r\n专属运营经理会在24小时内联系您',
      confirmText: "立即申请",
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '申请中',
            mask: true
          })
          wx.request({
            url: app.globalData.host + '/api/Distributor/RequestAccountBalance?uid=' + app.globalData.userInfo.Uid,
            success: function (res) {
              wx.hideLoading();
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '成功'
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  
  //金额验证
  checkMoney: function (obj){
    if(!obj){
      return false;
    }else{
      return typeof obj === 'number' && obj % 50 === 0;//是整数，则返回true，否则返回false
    }
  },

  //安卓手机现金直冲
  submitAccountBalanceOrder: function () {
    if (!this.checkMoney(this.data.inputMoney)){
      wx.showModal({
        title: '提示',
        content: '请输入50-9900整数',
        showCancel: false,
      })
      return
    }
    wx.showLoading({
      title: '充值中',
      mask: true
    })
    wx.request({
      url: app.globalData.host + '/api/Order/SubmitAccountBalanceOrder',
      data: {
        uid: app.globalData.userInfo.Uid,
        count: this.data.inputMoney/50
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          utils.wxpay(app, res.data.Data, 1)
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 帮好友充值
  toRechargeOthers: function(){
    wx.navigateTo({
      url: '/fenxiao/rechargeOther/index',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.GetDistributorInfo();
  },
 //打开弹框
 showShareDialog: function () {
  this.setData({
    showShare: !this.data.showShare
  })
},
hideShareDialog: function () {
  this.setData({
    showShare: !this.data.showShare
  })
},

})